package com.ch.mapdemo.util;
/**
 * 坐标是否在坐标组内（平行线反射弧的方式）
 * @param ALon
 * @param ALat
 * @param ps
 * @return
 */
public class PolygonDemo {

	public static void main(String[] args) {
		//多边形测试
//		String points = "126.946276,44.411044;126.94715,44.411485;126.947864,44.411044;126.947338,44.410504;126.946442,44.410354";
//		System.err.println("*****************************************");
//		System.err.println(isPtInPoly(126.946458,44.411374, points));
//		System.err.println(isPtInPoly(126.946855,44.411048, points));
//		System.err.println(isPtInPoly(126.947655,44.410845, points));
//		System.err.println(isPtInPoly(126.946743,44.41037, points));
//		System.err.println(isPtInPoly(126.946695,44.410925, points));
//		System.err.println(isPtInPoly(126.945761,44.410849, points));
//		System.err.println("*****************************************");
		//矩形测试
//		String points = "126.946684,44.411293;126.948395,44.411293;126.948395,44.410818;126.946684,44.410818";
//		System.err.println("*****************************************");
//		System.err.println(isPtInPoly(126.94626,44.411251, points));
//		System.err.println(isPtInPoly(126.947354,44.411128, points));
//		System.err.println(isPtInPoly(126.948384,44.411427, points));
//		System.err.println(isPtInPoly(126.948218,44.41091, points));
//		System.err.println(isPtInPoly(126.948524,44.410569, points));
//		System.err.println(isPtInPoly(126.947188,44.410872, points));
//		System.err.println(isPtInPoly(126.946834,44.410504, points));
//		System.err.println("*****************************************");
		//折线测试（不支持）
//		String points = "126.946737,44.411259;126.947231,44.411293;126.947488,44.410699;126.947231,44.410285";
//		System.err.println("*****************************************");
//		System.err.println(isPtInPoly(126.947016,44.411285, points));
//		System.err.println(isPtInPoly(126.947236,44.41086, points));
//		System.err.println(isPtInPoly(126.947344,44.410438, points));
//		System.err.println(isPtInPoly(126.948218,44.41091, points));
//		System.err.println(isPtInPoly(126.948524,44.410569, points));
//		System.err.println(isPtInPoly(126.947188,44.410872, points));
//		System.err.println(isPtInPoly(126.946834,44.410504, points));
//		System.err.println("*****************************************");
		

	}
	
	/**
	 * 坐标是否在坐标组内
	 * @param ALon
	 * @param ALat
	 * @param ps
	 * @return
	 */
	public static boolean isPtInPoly (double ALon , double ALat , String ps) {  
        int iSum, iCount, iIndex; 
        //分割坐标数组
        String[] points = ps.split(";");
        double dLon1 = 0, dLon2 = 0, dLat1 = 0, dLat2 = 0, dLon;  
        if (points.length < 3) {  
            return false;  
        }  
        iSum = 0;  
        iCount = points.length;  
        for (iIndex = 0; iIndex<iCount;iIndex++) {  
            if (iIndex == iCount - 1) {  
                dLon1 = Double.parseDouble(points[iIndex].split(",")[0]);  
                dLat1 = Double.parseDouble(points[iIndex].split(",")[1]);  
                dLon2 = Double.parseDouble(points[0].split(",")[0]);  
                dLat2 = Double.parseDouble(points[0].split(",")[1]);  
            } else {  
                dLon1 = Double.parseDouble(points[iIndex].split(",")[0]);  
                dLat1 = Double.parseDouble(points[iIndex].split(",")[1]);  
                dLon2 = Double.parseDouble(points[iIndex+1].split(",")[0]);  
                dLat2 = Double.parseDouble(points[iIndex+1].split(",")[1]);  
            }  
            // 以下语句判断A点是否在边的两端点的水平平行线之间，在则可能有交点，开始判断交点是否在左射线上  
            if (((ALat >= dLat1) && (ALat < dLat2)) || ((ALat >= dLat2) && (ALat < dLat1))) {  
                if (Math.abs(dLat1 - dLat2) > 0) {  
                    //得到 A点向左射线与边的交点的x坐标：  
                    dLon = dLon1 - ((dLon1 - dLon2) * (dLat1 - ALat) ) / (dLat1 - dLat2);  
                    // 如果交点在A点左侧（说明是做射线与 边的交点），则射线与边的全部交点数加一：  
                    if (dLon < ALon) {  
                        iSum++;  
                    }  
                }  
            }  
        }  
        if ((iSum % 2) != 0) {  
            return true;  
        }  
        return false;  
    } 

}
